import axios, { type AxiosResponse, type InternalAxiosRequestConfig } from "axios";
import { ElMessage, ElMessageBox } from "element-plus";

/**
 * 创建 axios 实例
 *  */ 
const service = axios.create({
    baseURL: import.meta.env.VITE_API_BASE_URL, // 代理路径
    timeout: 50000,
    headers: { "Content-Type": "application/json;charset=utf-8" },
    withCredentials: true, // 允许携带cookie，用于跨域认证
})

/**
 * 请求拦截器
 */
service.interceptors.request.use(
    (config: InternalAxiosRequestConfig) => {
        // 在请求发送之前做一些处理，比如添加 token 等
        const accessToken = localStorage.getItem('accessToken');
        if(accessToken){
            config.headers.Authorization = accessToken;
        }
        return config;
    },
    (error: any) => {
        return Promise.reject(error);
    }
);

/**
 * 响应拦截器
 */
service.interceptors.response.use(
    (response: AxiosResponse) => { 
        const { code, msg } = response.data;
        if(code === "000000"){
            return response.data;
        }
        // 响应数据为二进制流处理(Excel导出)
        if(response.data instanceof ArrayBuffer){
            return response;
        }
        ElMessage.error(msg || "系统出错");
        return Promise.reject(new Error(msg || "Error"));
    },
    (error: any) => {
        if(error.response.data){
            const { code, msg } = error.response.data;
            // token 过期,重新登录
            if(code === "A0230"){
                ElMessageBox.confirm("当前页面已失效，请重新登录","提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }).then(() => {
                    
                })
            } else {
                ElMessage.error(msg || "系统出错");
            }
        }
        return Promise.reject(error.message)
    }
)

/**
 * 导出 axios 实例
 */
export default service;